namespace Crucible.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class Initial : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.Nodes",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Title = c.String(nullable: false),
                        Published = c.Boolean(nullable: false),
                        AddedBy_UserId = c.Int(nullable: false),
                        DateAdded = c.DateTime(nullable: false),
                        Type = c.Int(nullable: false),
                        Link = c.String(),
                        Body = c.String(),
                        Attribution = c.String(),
                        DateOriginated = c.DateTime(),
                        Replacement_Id = c.Int(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Nodes", t => t.Replacement_Id)
                .ForeignKey("dbo.UserProfile", t => t.AddedBy_UserId, cascadeDelete: true)
                .Index(t => t.Replacement_Id)
                .Index(t => t.AddedBy_UserId);
            
            CreateTable(
                "dbo.UserProfile",
                c => new
                    {
                        UserId = c.Int(nullable: false, identity: true),
                        UserName = c.String(maxLength: 50),
                    })
                .PrimaryKey(t => t.UserId);
            
            CreateTable(
                "dbo.Comments",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Author_UserId = c.Int(nullable: false),
                        Body = c.String(nullable: false),
                        Parent_Id = c.Int(),
                        Node_Id = c.Int(nullable: false),
                        Score = c.Int(nullable: false),
                        DateAdded = c.DateTime(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.UserProfile", t => t.Author_UserId)
                .ForeignKey("dbo.Comments", t => t.Parent_Id)
                .ForeignKey("dbo.Nodes", t => t.Node_Id, cascadeDelete: true)
                .Index(t => t.Author_UserId)
                .Index(t => t.Parent_Id)
                .Index(t => t.Node_Id);
            
            CreateTable(
                "dbo.NodeLinks",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Parent_Id = c.Int(nullable: false),
                        Child_Id = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Nodes", t => t.Parent_Id, cascadeDelete: true)
                .ForeignKey("dbo.Nodes", t => t.Child_Id, cascadeDelete: false)
                .Index(t => t.Parent_Id)
                .Index(t => t.Child_Id);
            
            CreateTable(
                "dbo.UserDatas",
                c => new
                    {
                        UserId = c.Int(nullable: false),
                        EmailAddress = c.String(nullable: false),
                    })
                .PrimaryKey(t => t.UserId)
                .ForeignKey("dbo.UserProfile", t => t.UserId)
                .Index(t => t.UserId);
            
            CreateTable(
                "dbo.NodeVotes",
                c => new
                    {
                        Node_Id = c.Int(nullable: false),
                        Author_UserId = c.Int(nullable: false),
                        Agreement = c.Int(nullable: false),
                        Logic = c.Int(nullable: false),
                    })
                .PrimaryKey(t => new { t.Node_Id, t.Author_UserId })
                .ForeignKey("dbo.UserProfile", t => t.Author_UserId)
                .ForeignKey("dbo.Nodes", t => t.Node_Id, cascadeDelete: true)
                .Index(t => t.Author_UserId)
                .Index(t => t.Node_Id);
            
            CreateTable(
                "dbo.Objections",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Explanation = c.String(),
                        Fallacy_Id = c.Int(),
                        NodeVote_Node_Id = c.Int(),
                        NodeVote_Author_UserId = c.Int(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Fallacies", t => t.Fallacy_Id)
                .ForeignKey("dbo.NodeVotes", t => new { t.NodeVote_Node_Id, t.NodeVote_Author_UserId })
                .Index(t => t.Fallacy_Id)
                .Index(t => new { t.NodeVote_Node_Id, t.NodeVote_Author_UserId });
            
            CreateTable(
                "dbo.Fallacies",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Name = c.String(nullable: false),
                        Description = c.String(nullable: false),
                        Link = c.String(nullable: false),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.CommentVotes",
                c => new
                    {
                        Comment_Id = c.Int(nullable: false),
                        Voter_UserId = c.Int(nullable: false),
                        Vote = c.Int(nullable: false),
                    })
                .PrimaryKey(t => new { t.Comment_Id, t.Voter_UserId })
                .ForeignKey("dbo.Comments", t => t.Comment_Id, cascadeDelete: true)
                .ForeignKey("dbo.UserProfile", t => t.Voter_UserId)
                .Index(t => t.Comment_Id)
                .Index(t => t.Voter_UserId);
            
        }
        
        public override void Down()
        {
            DropIndex("dbo.CommentVotes", new[] { "Voter_UserId" });
            DropIndex("dbo.CommentVotes", new[] { "Comment_Id" });
            DropIndex("dbo.Objections", new[] { "NodeVote_Node_Id", "NodeVote_Author_UserId" });
            DropIndex("dbo.Objections", new[] { "Fallacy_Id" });
            DropIndex("dbo.NodeVotes", new[] { "Node_Id" });
            DropIndex("dbo.NodeVotes", new[] { "Author_UserId" });
            DropIndex("dbo.UserDatas", new[] { "UserId" });
            DropIndex("dbo.NodeLinks", new[] { "Child_Id" });
            DropIndex("dbo.NodeLinks", new[] { "Parent_Id" });
            DropIndex("dbo.Comments", new[] { "Node_Id" });
            DropIndex("dbo.Comments", new[] { "Parent_Id" });
            DropIndex("dbo.Comments", new[] { "Author_UserId" });
            DropIndex("dbo.Nodes", new[] { "AddedBy_UserId" });
            DropIndex("dbo.Nodes", new[] { "Replacement_Id" });
            DropForeignKey("dbo.CommentVotes", "Voter_UserId", "dbo.UserProfile");
            DropForeignKey("dbo.CommentVotes", "Comment_Id", "dbo.Comments");
            DropForeignKey("dbo.Objections", new[] { "NodeVote_Node_Id", "NodeVote_Author_UserId" }, "dbo.NodeVotes");
            DropForeignKey("dbo.Objections", "Fallacy_Id", "dbo.Fallacies");
            DropForeignKey("dbo.NodeVotes", "Node_Id", "dbo.Nodes");
            DropForeignKey("dbo.NodeVotes", "Author_UserId", "dbo.UserProfile");
            DropForeignKey("dbo.UserDatas", "UserId", "dbo.UserProfile");
            DropForeignKey("dbo.NodeLinks", "Child_Id", "dbo.Nodes");
            DropForeignKey("dbo.NodeLinks", "Parent_Id", "dbo.Nodes");
            DropForeignKey("dbo.Comments", "Node_Id", "dbo.Nodes");
            DropForeignKey("dbo.Comments", "Parent_Id", "dbo.Comments");
            DropForeignKey("dbo.Comments", "Author_UserId", "dbo.UserProfile");
            DropForeignKey("dbo.Nodes", "AddedBy_UserId", "dbo.UserProfile");
            DropForeignKey("dbo.Nodes", "Replacement_Id", "dbo.Nodes");
            DropTable("dbo.CommentVotes");
            DropTable("dbo.Fallacies");
            DropTable("dbo.Objections");
            DropTable("dbo.NodeVotes");
            DropTable("dbo.UserDatas");
            DropTable("dbo.NodeLinks");
            DropTable("dbo.Comments");
            DropTable("dbo.UserProfile");
            DropTable("dbo.Nodes");
        }
    }
}
